package Action.user;

import Action.Sys.Setting;
import Bean.BookInfoEntity;
import Bean.LoanBookLogRecord;
import Bean.Log.OrderLog;
import Bean.UserinfoEntity;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.hibernate.Session;
import tools.HibernateTools;
import tools.TimeTools;

import java.util.LinkedList;
import java.util.List;

public class MyOrder extends ActionSupport {
    List<LoanBookLogRecord> loanBookLogRecords;
    LinkedList<Object[]> data=new LinkedList<>();
    String key;
    String state;
    Session session;
    int usid;
    int modify_key;
    public String show_order() throws Exception{
        try {
            usid = (int) ActionContext.getContext().getSession().get("userid");
        } catch (Exception e) {
           return LOGIN;
        }
        loanBookLogRecords=session.createQuery("from LoanBookLogRecord where userId=? order by loanTime desc ")
                .setParameter(0, usid)
                .getResultList();
//        HibernateTools.commit(session);
//        build_order();
        return SUCCESS;
    }

    public String find_order() throws Exception{
        try {
            usid = (int) ActionContext.getContext().getSession().get("userid");
        } catch (Exception e) {
            return LOGIN;
        }
        ;
        session = HibernateTools.getSession();
        if (key==null||key.equals(""))
            show_order();
        else {
           show_order();
           LinkedList<LoanBookLogRecord> middle=new LinkedList<>();
            for (LoanBookLogRecord loanBookLogRecord : loanBookLogRecords) {
                if ((""+loanBookLogRecord.getBookId()).contains(key)){
                    middle.addLast(loanBookLogRecord);
                    continue;
                }if ((""+loanBookLogRecord.getState()).contains(key)){
                    middle.addLast(loanBookLogRecord);
                    continue;
                }if ((""+loanBookLogRecord.getUserId()).contains(key)){
                    middle.addLast(loanBookLogRecord);
                    continue;
                }
                //图书信息模糊搜索
                BookInfoEntity bookInfoEntity=session.get(BookInfoEntity.class, loanBookLogRecord.getBookId());
               if (bookInfoEntity!=null&&bookInfoEntity.getBook_name().contains(key)){
                   middle.addLast(loanBookLogRecord);
                   continue;
               }

            }
            loanBookLogRecords=middle;
        }

        if (state!=null&&!state.equals("")){
            filter_order();//订单过滤器
        }else {
            state="所有";
        }
        build_order();
//        HibernateTools.commit(session);
        return SUCCESS;
    }
    //为前台提供用户图书，借阅三个信息
    private void build_order() throws Exception{
        data.clear();
        Session session=HibernateTools.getSession();
        for (LoanBookLogRecord loanBookLogRecord : loanBookLogRecords) {
            String id=""+loanBookLogRecord.getId();
            BookInfoEntity bookInfoEntity = session.get(BookInfoEntity.class, loanBookLogRecord.getBookId());
            String bookname= bookInfoEntity==null?"已删除图书ID:"+loanBookLogRecord.getUserId():bookInfoEntity.getBook_name();
            UserinfoEntity userinfoEntity = session.get(UserinfoEntity.class, loanBookLogRecord.getUserId());
            String username= userinfoEntity==null?"已删除用户ID:"+loanBookLogRecord.getUserId():""+loanBookLogRecord.getUserId();
            String orderTime= TimeTools.timeStamp2Date(loanBookLogRecord.getLoanTime(),null);
            String state=loanBookLogRecord.getState();
            String day=""+loanBookLogRecord.getDay();
            Integer statepercentage=0;
            if (state.equals(Setting.LoanBook_state_closed)){
                statepercentage=100;
            }else if (state.equals(Setting.LoanBook_state_reservation))
            {
                statepercentage=25;
            }else if (state.equals(Setting.LoanBook_state_need_TimeOut)||state.equals(Setting.LoanBook_state_need_Return)){
                statepercentage=75;
            }else {
                statepercentage=100;//用户取消等情况
            }
            int remainingDays = loanBookLogRecord.remainingDays();
            if (remainingDays<0){
                remainingDays=0;
            }
            data.add(new Object[]{id,username,bookname,orderTime,day,remainingDays,state,statepercentage,loanBookLogRecord.getBookId()});

        }
        if (data.size()==0){
            addActionError("没有订单信息");
        }
        HibernateTools.commit(session);

    }

    private  String filter_order() throws Exception{//筛选订单信息
        if (state==null||state.equals("全部")){
            return SUCCESS;
        }

//        find_order();

        LinkedList<LoanBookLogRecord> middle=new LinkedList<>();
        for (LoanBookLogRecord loanBookLogRecord : loanBookLogRecords) {
            if (loanBookLogRecord.getState().equals(state)){
                middle.addLast(loanBookLogRecord);
            }
        }
        loanBookLogRecords=middle;
//        build_order();
        return SUCCESS;
    }
    public String cancel_order() throws Exception{
        Session session = HibernateTools.getSession();

        LoanBookLogRecord loanBookLogRecord=session.get(LoanBookLogRecord.class, modify_key);
        if (loanBookLogRecord==null)
        {
            addActionError("订单不存在");
            return find_order();
        }
        if (!loanBookLogRecord.getState().equals(Setting.LoanBook_state_reservation)){
            addActionError("此订单不可以取消");
            return find_order();
        }
        //正常逻辑
        loanBookLogRecord.setState(Setting.LoanBook_state_cancel);
        BookInfoEntity bookInfoEntity=session.get(BookInfoEntity.class, loanBookLogRecord.getBookId());
        bookInfoEntity.setUsed(bookInfoEntity.getUsed()-1);
        session.update(bookInfoEntity);
        session.update(loanBookLogRecord);
        addActionMessage("取消成功");

        OrderLog.addLog(loanBookLogRecord.getId(), "用户取消订单");
        HibernateTools.commit(session);
        return find_order();
    }

    public List<LoanBookLogRecord> getLoanBookLogRecords() {
        return loanBookLogRecords;
    }

    public void setLoanBookLogRecords(List<LoanBookLogRecord> loanBookLogRecords) {
        this.loanBookLogRecords = loanBookLogRecords;
    }



    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public List<Object[]> getData() {
        return data;
    }

    public void setData(LinkedList<Object[]> data) {
        this.data = data;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getState() {
        return state;
    }

    public int getModify_key() {
        return modify_key;
    }

    public void setModify_key(int modify_key) {
        this.modify_key = modify_key;
    }
}
